################################################################################
##
## BY: WANDERSON M.PIMENTA
## PROJECT MADE WITH: Qt Designer and PySide2
## V: 1.0.0
##
## This project can be used freely for all uses, as long as they maintain the
## respective credits only in the Python scripts, any information in the visual
## interface (GUI) can be modified without any implication.
##
## There are limitations on Qt licenses if you want to use your products
## commercially, I recommend reading them on the official website:
## https://doc.qt.io/qtforpython/licenses.html
##
################################################################################
import ctypes
import json
import sys
import platform
import threading
import time
from configparser import ConfigParser
from threading import Timer

import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect,
                          QSize, QTime, QUrl, Qt, QEvent, QThread)
from PyQt5.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence,
                         QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt5.QtWidgets import *
from PyQt5 import uic
from emergency import Emergency
from course import Course
from confrontation import Confrontation
from emergencyThread import EmergencyThread
from personal import Personal
from settings import Settings
from psychology import Psychology
from login import Login
from browser import Browser
from external import External
# GUI FILE
from app_modules import *


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # 读取系统配置
        self.readSettings()

        if self.colorStyle.upper() == 'WHITE':
            self.isWhite = True
            self.loadWhiteUI()
            self.setWhiteStyle()
        else:
            self.isWhite = False
            self.loadUI()

        # 用于维持和后端的连接
        self.session = requests.session()

        # 用于保存登陆后得到的cookie
        self.cookies = dict()

        # 课题四的token
        self.emergencyToken = None

        # 记录是否登录的状态
        self.isLogin = False

        # 记录是否开启轮询
        self.isPolling = False

        # 记录应急演练状态，0为已设置尚未开始，1为正在进行，2为已结束
        self.emergencyStatus = 2

        ## PRINT ==> SYSTEM
        print('System: ' + platform.system())
        print('Version: ' + platform.release())

        ########################################################################
        ## START - WINDOW ATTRIBUTES
        ########################################################################

        ## REMOVE ==> STANDARD TITLE BAR
        UIFunctions.removeTitleBar(True)
        ## ==> END ##

        ## SET ==> WINDOW TITLE
        self.setWindowTitle('Main Window - Python Base')
        UIFunctions.labelTitle(self, 'Main Window - Python Base')
        UIFunctions.labelDescription(self, 'Set text')
        ## ==> END ##

        ## WINDOW SIZE ==> DEFAULT SIZE
        startSize = QSize(1600, 900)
        self.resize(startSize)
        self.setMinimumSize(startSize)
        # UIFunctions.enableMaximumSize(self, 500, 720)
        ## ==> END ##

        ## ==> CREATE MENUS
        ########################################################################

        ## ==> TOGGLE MENU SIZE
        self.ui.btn_toggle_menu.clicked.connect(lambda: UIFunctions.toggleMenu(self, 220, True))
        ## ==> END ##

        ## ==> ADD CUSTOM MENUS
        self.ui.stackedWidget.setMinimumWidth(20)
        UIFunctions.addNewMenu(self, "登录", "btn_login", "url(:/16x16/icons/16x16/cil-people.png)", True, self.isWhite)
        UIFunctions.addNewMenu(self, "总界面", "btn_web", "url(:/16x16/icons/16x16/cil-home.png)", True, self.isWhite)
        # UIFunctions.addNewMenu(self, "HOME", "btn_home", "url(:/16x16/icons/16x16/cil-home.png)", True)
        UIFunctions.addNewMenu(self, "心理诱导", "btn_psychology", "url(:/16x16/icons/16x16/cil-user-follow.png)",
                               True, self.isWhite)
        UIFunctions.addNewMenu(self, "执法训练", "btn_course", "url(:/16x16/icons/16x16/cil-user-follow.png)",
                               True, self.isWhite)
        UIFunctions.addNewMenu(self, "情景对抗", "btn_scene", "url(:/16x16/icons/16x16/cil-user-follow.png)",
                               True, self.isWhite)
        UIFunctions.addNewMenu(self, "应急处置", "btn_emergency",
                               "url(:/16x16/icons/16x16/cil-user-follow.png)", True, self.isWhite)
        UIFunctions.addNewMenu(self, "个人信息", "btn_personal", "url(:/16x16/icons/16x16/cil-user.png)", True,
                               self.isWhite)
        # UIFunctions.addNewMenu(self, "外部程序", 'btn_external', "url(:/16x16/icons/16x16/cil-user-follow.png)", True,
        #                        self.isWhite)
        UIFunctions.addNewMenu(self, "退出", "btn_logout", 'url(:/20x20/icons/20x20/cil-exit-to-app.png)', False,
                               self.isWhite)
        UIFunctions.addNewMenu(self, "设置", "btn_settings", 'url(:/20x20/icons/20x20/cil-settings.png)', False,
                               self.isWhite)
        ## ==> END ##

        self.ui.frame_left_menu.findChild(QPushButton, "btn_logout").clicked.connect(self.logout)

        # 设置应急处置训练页面
        self.emergency = Emergency(self)

        # 设置情景对抗控制页面
        self.confrontation = Confrontation(self)

        # 设置心理训练页面
        self.psychology = Psychology(self)

        # 设置日常训练页面
        self.course = Course(self)

        # 设置登录页面
        self.login = Login(self)

        # 设置浏览器页面
        self.web = Browser(self)

        # 设置个人信息页面
        self.personal = Personal(self)

        # 设置软件设置页面
        self.settings = Settings(self)

        self.external = External(self)

        # 刚进入系统时实际上是未登录状态，隐藏除了登录外的其他按钮
        self.hideButtons()

        ## ==> MOVE WINDOW / MAXIMIZE / RESTORE
        ########################################################################
        def moveWindow(event):
            # IF MAXIMIZED CHANGE TO NORMAL
            if UIFunctions.returStatus() == 1:
                UIFunctions.maximize_restore(self)

            # MOVE WINDOW
            if event.buttons() == Qt.LeftButton:
                self.move(self.pos() + event.globalPos() - self.dragPos)
                self.dragPos = event.globalPos()
                event.accept()

        # WIDGET TO MOVE
        self.ui.frame_label_top_btns.mouseMoveEvent = moveWindow
        ## ==> END ##

        ## ==> LOAD DEFINITIONS
        ########################################################################
        UIFunctions.uiDefinitions(self)
        ## ==> END ##

        ########################################################################
        ## END - WINDOW ATTRIBUTES
        ############################## ---/--/--- ##############################

        ########################################################################
        #                                                                      #
        ## START -------------- WIDGETS FUNCTIONS/PARAMETERS ---------------- ##
        #                                                                      #
        ## ==> USER CODES BELLOW                                              ##
        ########################################################################

        ## ==> QTableWidget RARAMETERS
        ########################################################################
        self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        ## ==> END ##

        ########################################################################
        #                                                                      #
        ## END --------------- WIDGETS FUNCTIONS/PARAMETERS ----------------- ##
        #                                                                      #
        ############################## ---/--/--- ##############################

        ## SHOW ==> MAIN WINDOW
        ########################################################################
        self.show()
        ## ==> END ##

    ########################################################################
    ## MENUS ==> DYNAMIC MENUS FUNCTIONS
    ########################################################################
    def Button(self):
        # GET BT CLICKED
        btnWidget = self.sender()

        # PAGE HOME
        if btnWidget.objectName() == "btn_home":
            self.ui.stackedWidget.setCurrentWidget(self.ui.page_home)
            UIFunctions.resetStyle(self, "btn_home")
            UIFunctions.labelPage(self, "Home")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))

        # PAGE emergency
        if btnWidget.objectName() == "btn_emergency":
            self.ui.stackedWidget.setCurrentWidget(self.ui.emergency.page_widgets)
            UIFunctions.resetStyle(self, "btn_new_user")
            UIFunctions.labelPage(self, "New User")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))

            # 加载应急处置训练页面数据
            self.emergency.loadData()

        # PAGE WIDGETS
        if btnWidget.objectName() == "btn_scene":
            self.ui.stackedWidget.setCurrentWidget(self.ui.confrontation.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Custom Widgets")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))

            # 设置情景对抗训练页面
            self.confrontation.loadData()

        if btnWidget.objectName() == "btn_psychology":
            self.ui.stackedWidget.setCurrentWidget(self.ui.psychology.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Custom Widgets")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))
            self.psychology.loadData()

        if btnWidget.objectName() == "btn_course":
            self.ui.stackedWidget.setCurrentWidget(self.ui.course.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Custom Widgets")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))
            self.course.loadData()

        if btnWidget.objectName() == "btn_login":
            self.ui.stackedWidget.setCurrentWidget(self.ui.login.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Custom Widgets")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))
            self.login.loadData()

        if btnWidget.objectName() == "btn_web":
            self.ui.stackedWidget.setCurrentWidget(self.ui.web.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Home")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))
            self.web.loadData()

        if btnWidget.objectName() == "btn_personal":
            self.ui.stackedWidget.setCurrentWidget(self.ui.personal.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Custom Widgets")
            btnWidget.setStyleSheet(UIFunctions.selectMenu(btnWidget.styleSheet()))
            self.personal.loadData()

        if btnWidget.objectName() == "btn_settings":
            self.ui.stackedWidget.setCurrentWidget(self.ui.settings.page_widgets)
            UIFunctions.resetStyle(self, "btn_widgets")
            UIFunctions.labelPage(self, "Custom Widgets")
            self.settings.loadData()

        # if btnWidget.objectName() == 'btn_external':
        #     self.ui.stackedWidget.setCurrentWidget(self.ui.external.page_widgets)
        #     UIFunctions.resetStyle(self, 'btn_widgets')
        #     UIFunctions.labelPage(self, 'Custom Widgets')
        #     self.external.loadData()
        #     self.external.load_program(self.confrontation_path, self.confrontation_name)

    ## ==> END ##

    ########################################################################
    ## START ==> APP EVENTS
    ########################################################################

    ## EVENT ==> MOUSE DOUBLE CLICK
    ########################################################################
    def eventFilter(self, watched, event):
        if watched == self.le and event.type() == QtCore.QEvent.MouseButtonDblClick:
            print("pos: ", event.pos())

    ## ==> END ##

    ## EVENT ==> MOUSE CLICK
    ########################################################################
    def mousePressEvent(self, event):
        self.dragPos = event.globalPos()
        if event.buttons() == Qt.LeftButton:
            print('Mouse click: LEFT CLICK')
        if event.buttons() == Qt.RightButton:
            print('Mouse click: RIGHT CLICK')
        if event.buttons() == Qt.MidButton:
            print('Mouse click: MIDDLE BUTTON')

    ## ==> END ##

    ## EVENT ==> KEY PRESSED
    ########################################################################
    def keyPressEvent(self, event):
        print('Key: ' + str(event.key()) + ' | Text Press: ' + str(event.text()))

    ## ==> END ##

    ## EVENT ==> RESIZE EVENT
    ########################################################################
    def resizeEvent(self, event):
        self.resizeFunction()
        return super(MainWindow, self).resizeEvent(event)

    def resizeFunction(self):
        print('Height: ' + str(self.height()) + ' | Width: ' + str(self.width()))

    ## ==> END ##

    # 根据按钮名称隐藏按钮
    def enableButton(self, button):
        UIFunctions.enableButton(self, button)

    # 根据按钮名称显示按钮
    def disableButton(self, button):
        UIFunctions.disableButton(self, button)

    ########################################################################
    ## END ==> APP EVENTS
    ############################## ---/--/--- ##############################

    def logout(self):
        # 如果是退出系统，则执行注销操作
        self.session.post(self.url + '/passport/logout')
        self.isLogin = False
        # 结束轮询
        self.isPolling = False
        self.hideButtons()

    def hideButtons(self):
        # 未登录时隐藏除了登录外的其他按钮
        UIFunctions.disableButton(self, "btn_home")
        UIFunctions.disableButton(self, "btn_emergency")
        UIFunctions.disableButton(self, "btn_scene")
        UIFunctions.disableButton(self, "btn_psychology")
        UIFunctions.disableButton(self, "btn_course")
        UIFunctions.disableButton(self, "btn_web")
        UIFunctions.disableButton(self, "btn_widgets")
        UIFunctions.disableButton(self, "btn_login")
        UIFunctions.disableButton(self, "btn_personal")
        # UIFunctions.disableButton(self, 'btn_external')
        # 隐藏左下角退出按钮
        UIFunctions.disableButton(self, "btn_logout")

        # 隐藏左下角显示用户名的标志
        self.ui.label_user_icon.setHidden(True)

        # START MENU => SELECTION
        # 设置进入时显示的是登录页面
        UIFunctions.selectStandardMenu(self, "btn_login")
        ## ==> END ##

        ## ==> START PAGE
        # 设置进入时显示的是登录页面
        self.ui.stackedWidget.setCurrentWidget(self.ui.login.page_widgets)
        ## ==> END ##

    def readSettings(self):
        path = './settings.ini'
        config = ConfigParser()
        config.read(filenames=path, encoding='utf8')
        try:
            settings = config['flask_qt']
            try:
                self.address = settings['address']
            except:
                self.address = '127.0.0.1'
            try:
                self.port = int(settings['port'])
            except:
                self.port = 5000
            try:
                self.colorStyle = settings['style']
            except:
                self.colorStyle = 'DARK'
            self.url = 'http://' + str(self.address) + ':' + str(self.port)

        except:
            self.address = '127.0.0.1'
            self.port = '5000'
            self.url = 'http://127.0.0.1:5000'
            self.colorStyle = 'DARK'
        # 获取课题四的登录信息
        try:
            emergencySettings = config['emergency']
            try:
                self.emergencyAddress = emergencySettings['address']
            except:
                self.emergencyAddress = '127.0.0.1'

            try:
                self.emergencyPort = int(emergencySettings['port'])
            except:
                self.emergencyAddress = 35000

            try:
                self.emergencyUsername = emergencySettings['username']
            except:
                self.emergencyUsername = "superadmin"

            try:
                self.emergencyPassword = emergencySettings['password']
            except:
                self.emergencyPassword = "12345678"
            self.emergencyUrl = 'http://' + str(self.emergencyAddress) + ':' + str(self.emergencyPort)

        except:
            self.emergencyAddress = "127.0.0.1"
            self.emergencyPort = 35000
            self.emergencyUsername = "superadmin"
            self.emergencyPassword = "12345678"
            self.emergencyUrl = 'http://' + str(self.emergencyAddress) + ':' + str(self.emergencyPort)

        psychology_control_address = config['psychology']['control_address']
        psychology_control_port = config['psychology']['control_port']
        psychology_web_address = config['psychology']['web_address']
        psychology_web_port = config['psychology']['web_port']
        psychology_socket_address = config['psychology']['socket_address']
        psychology_socket_port = config['psychology']['socket_port']
        psychology_ue_address = config['psychology']['ue_address']
        psychology_ue_port = config['psychology']['ue_port']
        self.psychology_program_path = config['psychology']['program_path']
        self.psychology_shop_path = config['psychology']['shop_path']
        self.psychology_control_url = 'http://' + str(psychology_control_address) + ':' + str(psychology_control_port)
        self.psychology_web_url = 'http://' + str(psychology_web_address) + ':' + str(psychology_web_port)
        self.psychology_socket_url = 'http://' + str(psychology_socket_address) + ':' + str(psychology_socket_port)
        self.psychology_ue_url = 'http://' + str(psychology_ue_address) + ':' + str(psychology_ue_port)

        self.confrontation_path = config['confrontation']['path']
        self.confrontation_name = config['confrontation']['name']

        self.confrontation_escape_path = config['confrontation']['escape_path']
        self.confrontation_escape_name = config['confrontation']['escape_name']
        self.confrontation_escape_setting = config['confrontation']['escape_setting']
        self.confrontation_training_path = config['confrontation']['training_path']
        self.confrontation_training_name = config['confrontation']['training_name']
        self.confrontation_training_setting = config['confrontation']['training_setting']
        self.confrontation_equipment_path = config['confrontation']['equipment_path']
        self.confrontation_equipment_name = config['confrontation']['equipment_name']
        self.confrontation_crew_path = config['confrontation']['crew_path']
        self.confrontation_crew_name = config['confrontation']['crew_name']
        self.confrontation_model_path = config['confrontation']['model_path']
        self.confrontation_model_name = config['confrontation']['model_name']

        self.course_standard_path = config['course']['standard_path']
        self.course_standard_name = config['course']['standard_name']
        self.course_response_path = config['course']['response_path']
        self.course_response_name = config['course']['response_name']
        self.course_emotion_path = config['course']['emotion_path']
        self.course_emotion_name = config['course']['emotion_name']
        self.course_psychology_path = config['course']['psychology_path']
        self.course_psychology_name = config['course']['psychology_name']

    # 重启软件
    def restart(self):
        self.close()
        self.mainWindow = MainWindow()

    # 加载黑色页面
    def loadUI(self):
        self.ui.login = uic.loadUi('ui/loginPage.ui')
        self.ui.stackedWidget.addWidget(self.ui.login.page_widgets)

        self.ui.personal = uic.loadUi('ui/personal.ui')
        self.ui.stackedWidget.addWidget(self.ui.personal.page_widgets)

        self.ui.emergency = uic.loadUi('ui/emergency.ui')
        self.ui.stackedWidget.addWidget(self.ui.emergency.page_widgets)

        self.ui.confrontation = uic.loadUi('ui/confrontation.ui')
        self.ui.stackedWidget.addWidget(self.ui.confrontation.page_widgets)

        self.ui.psychology = uic.loadUi('ui/psychology.ui')
        self.ui.stackedWidget.addWidget(self.ui.psychology.page_widgets)

        self.ui.course = uic.loadUi('ui/course.ui')
        self.ui.stackedWidget.addWidget(self.ui.course.page_widgets)

        self.ui.web = uic.loadUi('ui/browser.ui')
        self.ui.stackedWidget.addWidget(self.ui.web.page_widgets)

        self.ui.settings = uic.loadUi('ui/settings.ui')
        self.ui.stackedWidget.addWidget(self.ui.settings.page_widgets)

        self.ui.external = uic.loadUi('ui/program.ui')
        self.ui.stackedWidget.addWidget(self.ui.external.page_widgets)

    # 加载白色页面
    def loadWhiteUI(self):
        self.ui.login = uic.loadUi('ui/loginPage_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.login.page_widgets)

        self.ui.personal = uic.loadUi('ui/personal_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.personal.page_widgets)

        self.ui.emergency = uic.loadUi('ui/emergency_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.emergency.page_widgets)

        self.ui.confrontation = uic.loadUi('ui/confrontation_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.confrontation.page_widgets)

        self.ui.psychology = uic.loadUi('ui/psychology_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.psychology.page_widgets)

        self.ui.course = uic.loadUi('ui/course_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.course.page_widgets)

        self.ui.web = uic.loadUi('ui/browser.ui')
        self.ui.stackedWidget.addWidget(self.ui.web.page_widgets)

        self.ui.settings = uic.loadUi('ui/settings_white.ui')
        self.ui.stackedWidget.addWidget(self.ui.settings.page_widgets)

        self.ui.external = uic.loadUi('ui/program.ui')
        self.ui.stackedWidget.addWidget(self.ui.external.page_widgets)

    # 设置界面为白色
    def setWhiteStyle(self):
        # 设置左侧栏为蓝色
        self.ui.frame_left_menu.setStyleSheet(WhiteStyles.frame_left_menu_style)
        # 设置左上角展开按钮处为蓝色
        self.ui.frame_toggle.setStyleSheet(WhiteStyles.frame_toggle_style)
        self.ui.frame_top_btns.setStyleSheet(WhiteStyles.frame_top_btns_style)
        # 设置窗口边框为灰色
        self.ui.btn_toggle_menu.setStyleSheet(WhiteStyles.btn_toggle_menu_style)
        # 设置标题颜色为黑色
        self.ui.label_title_bar_top.setStyleSheet(u'color:rgb(0,0,0);');
        # 设置上方信息栏为灰色
        self.ui.frame_top_info.setStyleSheet(WhiteStyles.frame_top_info_style)
        # 设置下方信息栏为灰色
        self.ui.frame_grip.setStyleSheet(WhiteStyles.frame_grip_style)
        # 设置背景为白色
        self.ui.frame_center.setStyleSheet(WhiteStyles.background_white)
        self.ui.frame_content_right.setStyleSheet(WhiteStyles.background_white)
        # 设置图标为黑色图标
        # 最小化按钮的图标
        icon_minimize = QIcon()
        icon_minimize.addFile(u":/black/icons/black/减_minus.svg", QSize(), QIcon.Normal, QIcon.Off)
        self.ui.btn_minimize.setIcon(icon_minimize)
        # 最大化按钮的图标
        icon_maximize = QIcon()
        icon_maximize.addFile(u':/black/icons/black/方形_square.svg', QSize(), QIcon.Normal, QIcon.Off)
        self.ui.btn_maximize_restore.setIcon(icon_maximize)
        # 关闭按钮的图标
        icon_close = QIcon()
        icon_close.addFile(u':/black/icons/black/关闭_close.svg', QSize(), QIcon.Normal, QIcon.Off)
        self.ui.btn_close.setIcon(icon_close)

    def thread_check_emergency_status(self):
        self.emergency.end_training()
        self.emergencyThread = EmergencyThread(url=self.url + '/emergency/status')
        self.emergencyThread.load_status.connect(self.check_emergency_status)
        self.emergencyThread.start()

    def check_emergency_status(self, data):
        if data.get('status') == 0:
            if self.emergencyStatus != 0:
                self.emergency.start_training()
                self.emergencyStatus = 0
                self.newMessage(u"训练已开始，请前往编辑训练设置")
        elif data.get('status') == 1:
            if self.emergencyStatus != 1:
                self.emergency.end_training()
                self.emergencyStatus = 1
        elif data.get('status') == 2:
            if self.emergencyStatus != 2:
                self.emergencyStatus = 2
                self.emergency.end_training()
                self.newMessage(u"训练已结束，请前往查看评分信息")

    def newMessage(self, message):
        msg_box = QMessageBox(QMessageBox.Information, '提示', message)
        msg_box.exec_()

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        if hasattr(self.external, 'external_process'):
            ctypes.windll.user32.PostMessageW(self.external.external_process._handle, 0x0010, 0, 0)  # 关闭外部窗口
            a0.accept()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    QtGui.QFontDatabase.addApplicationFont('fonts/segoeui.ttf')
    QtGui.QFontDatabase.addApplicationFont('fonts/segoeuib.ttf')
    window = MainWindow()
    sys.exit(app.exec_())
